namespace Terra.Core.Common.ErrorCodes.Attributes;

using Terra.Core.Common.ErrorCodes.Models;

/// <summary>
/// 错误码特性
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class ErrorCodeAttribute : Attribute
{
    /// <summary>
    /// 错误码
    /// </summary>
    public string Code { get; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string Message { get; }

    /// <summary>
    /// 错误级别
    /// </summary>
    public ErrorSeverity Severity { get; set; } = ErrorSeverity.Error;

    /// <summary>
    /// 解决方案
    /// </summary>
    public string Solution { get; set; }

    /// <summary>
    /// 初始化错误码特性
    /// </summary>
    /// <param name="code">错误码</param>
    /// <param name="message">错误消息</param>
    public ErrorCodeAttribute(string code, string message)
    {
        Code = code;
        Message = message;
    }

    /// <summary>
    /// 转换为错误码信息
    /// </summary>
    /// <returns>错误码信息</returns>
    public ErrorCodeInfo ToErrorCodeInfo()
    {
        return new ErrorCodeInfo
        {
            Code = Code,
            Message = Message,
            Severity = Severity,
            Solution = Solution
        };
    }
}